import { defaultApi } from '../http/apiFactory'
import moment from 'moment'
// competing_store_blogger_controller
export default class CompetingStoreBloggerApi {

  // Store Blogger Report
  static storeBloggerReport(data = {}) {
    return defaultApi.post('/chat-api/v1/mct/competing-store-blogger/store_blogger-report', data)
  }

  // User Blogger Report
  static userBloggerReport(data = {}) {
    return defaultApi.post('/chat-api/v1/mct/competing-store-blogger/user_blogger-report', data)
  }

  // Store Blogger Report Daily
  static storeBloggerReportDaily({ register_date }) {
    const date = moment(register_date[0])
    const endDate = moment(register_date[1])
    const dateList = []
    while (date.isSameOrBefore(endDate)) {
      dateList.push(date.format('YYYY-MM-DD'))
      date.add(1, 'days')
    }
    return defaultApi.post('/chat-api/v1/mct/competing-store-blogger/store_blogger-report-daily', { register_date }).then(res => {
      return {
        dataList: res,
        dateList
      }
    })
  }

  // User Blogger Report Daily
  static userBloggerReportDaily({ register_date }) {
    const date = moment(register_date[0])
    const endDate = moment(register_date[1])
    const dateList = []
    while (date.isSameOrBefore(endDate)) {
      dateList.push(date.format('YYYY-MM-DD'))
      date.add(1, 'days')
    }
    return defaultApi.post('/chat-api/v1/mct/competing-store-blogger/user_blogger-report-daily', { register_date }).then(res => {
      return {
        dataList: res,
        dateList
      }
    })
  }

  // Daily Store Blogger Report
  static dailyStoreBloggerReport(data = {}) {
    return defaultApi.post('/chat-api/v1/mct/competing-store-blogger/daily_store_blogger_report', data)
  }

  // Daily User Blogger Report
  static dailyUserBloggerReport(data = {}) {
    return defaultApi.post('/chat-api/v1/mct/competing-store-blogger/daily_user_blogger_report', data)
  }

  // Month Store Blogger Report
  static monthStoreBloggerReport({ register_date }) {
    const date = moment(register_date[0])
    const endDate = moment(register_date[1])
    const dateList = []
    while (date.isSameOrBefore(endDate)) {
      dateList.push(date.format('YYYY-MM-DD'))
      date.add(1, 'days')
    }
    return defaultApi.post('/chat-api/v1/mct/competing-store-blogger/month_store_blogger_report', { register_date }).then(res => {
      const dates = {}

      if (res.length > 0) {
        dateList.forEach(date => {
          dates[date] = res.map(el => el.dates[date] || 0).reduce((acc, cur) => acc + cur, 0)
        })
        res.push({
          'store_name': '总合计',
          dates,
          total: res.map(el => el.total).reduce((acc, cur) => acc + cur, 0)
        })
      }
      return {
        dataList: res,
        dateList
      }
    })
  }

  // Month User Blogger Report
  static monthUserBloggerReport({ register_date }) {
    const date = moment(register_date[0])
    const endDate = moment(register_date[1])
    const dateList = []
    while (date.isSameOrBefore(endDate)) {
      dateList.push(date.format('YYYY-MM-DD'))
      date.add(1, 'days')
    }
    return defaultApi.post('/chat-api/v1/mct/competing-store-blogger/month_user_blogger_report', { register_date }).then(res => {
      const dates = {}

      if (res.length > 0) {
        dateList.forEach(date => {
          dates[date] = res.map(el => el.dates[date] || 0).reduce((acc, cur) => acc + cur, 0)
        })
        res.push({
          'user_name': '总合计',
          dates,
          total: res.map(el => el.total).reduce((acc, cur) => acc + cur, 0)
        })
      }

      return {
        dataList: res,
        dateList
      }
    })
  }
}
